{extend name="base"}
{block name="style"}
<!-- 这里允许在引入一些样式头部 -->
<meta name="renderer" content="webkit">
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
<meta name="apple-mobile-web-app-status-bar-style" content="black">
<meta name="apple-mobile-web-app-capable" content="yes">
<meta name="format-detection" content="telephone=no">
<link rel="stylesheet" href="{:_static()}/layui/css/layui.css" media="all" />
<link rel="stylesheet" href="//at.alicdn.com/t/font_tnyc012u2rlwstt9.css" media="all" />
<link rel="stylesheet" href="{:_static()}/css/source.css" media="all" />
<link rel="stylesheet" href="{:_static()}/css/common.css" media="all" />

{/block}

{block name="main"}
<style type="text/css">
	.ws-main{padding: 15px;}
	a:hover{color: inherit !important;}
	label{box-sizing: content-box;}
</style>

	<div class="childrenBody">
    <blockquote class="layui-elem-quote news_search">
        <div class="layui-inline layui-form">
            <a class="layui-btn layui-btn-normal sourceDelete_btn">删除</a>
            <a class="layui-btn layui-btn-normal sourceAdd_btn">新增素材小组</a>
        </div>
    </blockquote>
    <div class="layui-form">
        <table id="cate-tb" class="layui-table">
            <thead>
                <tr>
                    <th><input type="checkbox" name="" lay-skin="primary" lay-filter="allChoose"></th>
                    <th>编号</th>
                    <th>素材组</th>
                    <th>数量</th>
                    <th>操作</th>
                </tr> 
            </thead>
            <tbody id="cmainhtml">
                {volist name="list" id="data"  empty="暂时没有数据" }
                <tr>
                    <td><input type="checkbox" name="cdelcheck" value="{$data.id}" lay-skin="primary"></td>
                    <td>{$data.id}</td>
                    <td>{$data.cate_name}</td>
                    <td>{$data.quant}</td>
                    <td>
                        <a class="layui-btn layui-btn-normal layui-btn-mini" href="{:url('Member/Material/manage',array('cates'=>$data.id))}"><i class="layui-icon"></i>查看</a>
                        <button class="layui-btn layui-btn-danger layui-btn-mini" onclick="cdel({$data.id});"><i class="layui-icon"></i>删除</button>
                    </td>
                </tr>
                {/volist}
            </tbody>
        </table>


        <table id="source-tb" class="layui-table" style="display: none;">
            <thead>
                <tr>
                    <th><input type="checkbox" name="" lay-skin="primary" lay-filter="allChoose"></th>
                    <th>编号</th>
                    <th>属性</th>
                    <th>素材小组</th>
                    <th>标题</th>
                    <th>文字内容</th>
                    <th>内容</th>
                    <th>评论内容</th>
                    <th>操作</th>
                </tr> 
            </thead>
            <tbody id="mainhtml">
            </tbody>
        </table>
        <!-- 分页 -->
        <div id="pagination1"></div>
    </div>
    <div id="page"></div>
	</div>

<script type="text/javascript" src="{:_static()}/layui/layui.js"></script>
<script type="text/babel" type="text/javascript" src="{:_static()}/js/common.js"></script>
<script type="text/babel" type="text/javascript">
    layui.config({
        base : "js/"
    }).use(['form','layer','jquery','laypage'],function(){
        var form = layui.form(),
            layer = parent.layer === undefined ? layui.layer : parent.layer,
            laypage = layui.laypage,
            $ = layui.jquery;


        //添加素材小组
        $(".sourceAdd_btn").click(function(){
            layer.prompt({title: '新建小组'}, function(val, index){
                // layer.msg('得到了'+val);
                // layer.close(index);
                //loading层
                var index2 = layer.load(1);
                // ajax提交添加
                $.ajax({
                    url:'{:url("member/material/addcate")}',
                    type:'GET',
                    data:{'cate_name':val},
                    dataType:'json',
                    success:function(data){
                        console.log(data)
                        // 关闭loading
                        layer.close(index2);
                        // 根据返回结果提示操作是否成功
                        if (data.code == 1) {
                            // 添加成功
                            // 关闭输入弹窗
                            layer.close(index);
                            layer.msg('添加素材小组成功');

                            // layer.msg(data.msg);

                            if (data.wait != '') {
                                ttime = data.wait*1000;
                            }
                            else{
                                ttime = 2000;
                            }
                            // 刷新当前页
                            window.setTimeout("window.location.reload()",ttime); 
                        }
                        else{
                            // 添加失败
                            layer.msg('添加素材小组失败');
                        }
                    }
                });
            });
        })

        //全选
        form.on('checkbox(allChoose)', function(data){
            var child = $(data.elem).parents('table').find('tbody input[type="checkbox"]:not([name="show"])');
            child.each(function(index, item){
                item.checked = data.elem.checked;
            });
            form.render('checkbox');
        });

        //通过判断文章是否全部选中来确定全选按钮是否选中
        form.on("checkbox(choose)",function(data){
            var child = $(data.elem).parents('table').find('tbody input[type="checkbox"]:not([name="show"])');
            var childChecked = $(data.elem).parents('table').find('tbody input[type="checkbox"]:not([name="show"]):checked')
            if(childChecked.length == child.length){
                $(data.elem).parents('table').find('thead input#allChoose').get(0).checked = true;
            }else{
                $(data.elem).parents('table').find('thead input#allChoose').get(0).checked = false;
            }
            form.render('checkbox');
        })

        //是否展示
        form.on('switch(isShow)', function(data){
            var index = layer.msg('修改中，请稍候',{icon: 16,time:false,shade:0.8});
            setTimeout(function(){
                layer.close(index);
                layer.msg("展示状态修改成功！");
            },2000);
        })
     
        //操作
        $("body").on("click",".news_edit",function(){  //编辑
            layer.alert('您点击了文章编辑按钮，由于是纯静态页面，所以暂时不存在编辑内容，后期会添加，敬请谅解。。。',{icon:6, title:'文章编辑'});
        })

        $("body").on("click",".news_collect",function(){  //收藏.
            if($(this).text().indexOf("已收藏") > 0){
                layer.msg("取消收藏成功！");
                $(this).html("<i class='layui-icon'>&#xe600;</i> 收藏");
            }else{
                layer.msg("收藏成功！");
                $(this).html("<i class='iconfont icon-star'></i> 已收藏");
            }
        })

        // 批量删除
        $('body').on('click','.sourceDelete_btn',function(){
            layer.confirm('在删除素材小组时，该小组里的素材将会一并删除！您确定要删除选中的素材小组吗？', {
                btn: ['确定', '取消']
                ,yes: function(index, layero){
                    // 获取选中的复选框
                    var id = '';
                    // var standardsName = '';
                    $("input:checkbox[name='cdelcheck']:checked").each(function() { // 遍历name=delcheck的多选框
                        id += $(this).val() + ',';
                        console.log(this);
                        // standardsName += ',' + $(this).attr('title');
                    });
                    // 删除第一个字符
                    id = id.substring(0, id.length - 1); 
                    console.log(id)
                    //loading层
                    var index2 = layer.load(1);
                    // ajax提交添加
                    $.ajax({
                        url:'{:url("member/material/delcate")}',
                        type:'POST',
                        data:{'id':id},
                        dataType:'json',
                        success:function(data){
                            console.log(data)
                            // 关闭loading
                            layer.close(index2);
                            // 根据返回结果提示操作是否成功
                            if (data.code == 1) {
                                // 添加成功
                                // 关闭输入弹窗
                                // layer.close(index);
                                layer.msg(data.msg);

                                if (data.wait != '') {
                                    ttime = data.wait*1000;
                                }
                                else{
                                    ttime = 2000;
                                }
                                // 刷新当前页
                                window.setTimeout("window.location.reload()",ttime); 
                                // window.location.reload()
                            }
                            else{
                                // 添加失败
                                layer.msg(data.msg);
                            }
                        }
                    });
                }
            }, function(index){
                //按钮【取消】的回调
            });
        })

        

        //分页
        laypage({
            cont: 'pagination1'
            ,pages: {$page.last_page} //总页数
            ,curr : {$page.current_page}//当前页
            ,groups: 5 //连续显示分页数
            ,jump: function(obj, first){
                //得到了当前页，用于向服务端请求对应数据
                var curr = obj.curr;
                // console.log(first)//异步请求返回给后台
                if (!first) {
                    $.ajax({
                        url:'{:url("member/material/classify")}',
                        type:'GET',
                        data:{'page':curr},
                        dataType:'json',
                        success:function(data){
                            // console.log(data)
                            // console.log(curr)
                            var htmls = '';

                            // 循环获取数据
                            $.each(data.data,function(j,item){
                                // console.log(item);
                                // console.log(j);
                                htmls += '<tr>';
                                    htmls += '<td><input type="checkbox" value="'+item.id+'" name="cdelcheck" lay-skin="primary"></td>';
                                    htmls += '<td>'+item.id+'</td>';
                                    htmls += '<td>'+item.cate_name+'</td>';
                                    htmls += '<td>'+item.quant+'</td>';
                                    htmls += '<td>';
                                        htmls += '<a href="/Member/material/manage/cates/'+item.id+'.html" class="layui-btn layui-btn-normal layui-btn-mini"><i class="layui-icon"></i>查看</a>';
                                        htmls += '<button class="layui-btn layui-btn-danger layui-btn-mini" onclick="cdel('+item.id+');"><i class="layui-icon"></i>删除</button>';
                                    htmls += '</td>';
                                htmls += '</tr>';

                            })
                            //这里获取到数据执行显示
                            $("#cmainhtml").html(htmls)
                            form.render('checkbox');
                            // $('#mainhtml').on('ifChanged', function(event) {
                            //     var $input = $('#mainhtml tr td').find('input');
                            //     $input.iCheck(event.currentTarget.checked ? 'check': 'uncheck');
                            // });
                        }
                    });
                }
            }
        });
    })


    // 删除
    function cdel(id){
        layer.confirm('在删除素材小组时，该小组里的素材将会一并删除！您确定要删除这个素材小组吗？', {
            btn: ['确定', '取消']
            ,yes: function(index, layero){
                
                //loading层
                var index2 = layer.load(1);
                // ajax提交添加
                $.ajax({
                    url:'{:url("member/material/delcate")}',
                    type:'POST',
                    data:{'id':id},
                    dataType:'json',
                    success:function(data){
                        console.log(data)
                        // 关闭loading
                        layer.close(index2);
                        // 根据返回结果提示操作是否成功
                        if (data.code == 1) {
                            // 添加成功
                            // 关闭输入弹窗
                            // layer.close(index);
                            layer.msg(data.msg);

                            if (data.wait != '') {
                                ttime = data.wait*1000;
                            }
                            else{
                                ttime = 2000;
                            }
                            // 刷新当前页
                            window.setTimeout("window.location.reload()",ttime); 
                            // window.location.reload()
                        }
                        else{
                            // 添加失败
                            layer.msg(data.msg);
                        }
                    }
                });
            }
        }, function(index){
            //按钮【取消】的回调
        });
        // alert(id);
    }
</script>
{/block}